var app = getApp();
var req = require("../../utils/req.js");
var url = require("../../utils/url.js");
// pages/addEmployee/addEmployee.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    level: ['员工'], //级别
    index: 0,
    type: '',
    name: '',
    stype: 2,
    phone: '',
    storeUserId: ''
  },
  bindPickerChange(e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var stype = 2;
    if (e.detail.value != 0) {
      this.setData({
        stype:1,
        index:1
      })
    }
  },
  /**
   * 添加员工
   */
  addEmployee: function() {
    var that = this;
    var storeUserId=that.data.storeUserId
    var name = that.data.name;
    var phone = that.data.phone;
    if (storeUserId==''){
      wx.showToast({
        icon:'none',
        title: '请输入正确的ID',
      })
      return
    }else if(name==''){
      wx.showToast({
        icon: 'none',
        title: '请输入正确的用户名',
      })
      return
    }else if(phone==''){
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号码',
      })
      return
    }else if(!that.checkMobile(phone)){
      return;
    }
    var storeUserId = that.data.storeUserId;
    var type = that.data.stype;
    var data = {
      storeUserId: storeUserId,
      phone: phone,
      name: name,
      type: type
    }
    var storeId = app.globalData.storeId;
    req.storeRequest("请稍等", "POST", url.store['addStaff'], storeId, data, function back(res) {
      console.log(res)
      wx.showToast({
        title: res.msg,
      })
    });
  },
  /**
   * 验证手机号
   */
  checkMobile:function(phone){
    if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))){
      wx.showModal({
        title: '提示',
        content: '手机号码不合法',
      })
      return false;
    }
    return true;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var type = options.type;
    if (type == 0) {
      that.setData({
        level: ['员工', '店长']
      })
    }
  },
  /**
   * 用户名
   */
  trueNameInput: function(e) {
    var name = e.detail.value;
    this.setData({
      name: name
    })
  },
  /**
   * 用户id
   */
  userNumberInput: function(e) {
    var storeUserId = e.detail.value;
    this.setData({
      storeUserId: storeUserId
    })
  },
  /**
   * 手机号码
   */
  phoneNumberInput: function(e) {
    var phone = e.detail.value;
    this.setData({
      phone: phone
    })
  },

})